package com.base.util;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.base.control.MActivitySupport;

import java.util.Timer;
import java.util.TimerTask;

public class Helper {
    public static boolean isInputMethodShow = false;

	public static void toast(Context context, Object msg) {
		Toast.makeText(context, msg.toString(), Toast.LENGTH_SHORT).show();
	}

    public static void getInputMethod(final EditText editText) {
        editText.setEnabled(true);
        editText.requestFocus();
        editText.setSelection(editText.length());
        Timer timer = new Timer();
        timer.schedule(
                new TimerTask() {
                    public void run() {
                        InputMethodManager inputManager =
                                (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }, 500);
    }

    public static void toggleInputMethod(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public static void hiddenInputMethod(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
    }

    public static void hintLogin(Context context) {
        toast(context, "请先登录");
    }

    private static long exitTime = 0;
    public static void exit(MActivitySupport context) {
        if((System.currentTimeMillis()-exitTime) > 2000){
            Helper.toast(context, "再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            context.finish();
            System.exit(0);
        }

    }

}
